#include <nds.h>
#include "sprites.h"
#include <assert.h>
#include <stdio.h>
#include "ellen/ln.h"
#include "mainmenu.h"
#include "game.h"

//Backgrounds
#include "splashScreen.h"
#include "cave.h"
#include "forest.h"

static const int SpriteDmaChannel = 3;

void initVideo(){
vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000,
				 VRAM_B_MAIN_BG_0x06020000,
				 VRAM_C_SUB_BG_0x06200000,
				 VRAM_D_LCD);

//AKS
//VRAM_E is mapped to MAIN_SPRITE in sprites.cpp

videoSetMode(MODE_4_2D |
			 DISPLAY_BG0_ACTIVE |
			 DISPLAY_BG2_ACTIVE |
			 DISPLAY_BG3_ACTIVE |
			 DISPLAY_SPR_ACTIVE |
			 DISPLAY_SPR_1D);

vramSetBankC(VRAM_C_SUB_BG);
videoSetModeSub(MODE_5_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG3_ACTIVE);

//AKS
//Initilize the sb screen background for text
SUB_BG0_CR = BG_MAP_BASE(2) | BG_PRIORITY(0);
//BG_PALETTE_SUB[255] = RGB15(31,31,31);
BG_PALETTE_SUB[255] = RGB15(0,0,0);
consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(2), (u16*)CHAR_BASE_BLOCK_SUB(0), 16);
}



void initBackgrounds(){

	BG3_CR = BG_BMP16_256x256 |
			 BG_BMP_BASE(8) |
			 BG_WRAP_ON |
			 BG_PRIORITY(3);

	BG3_XDX = 1 << 8;
	BG3_XDY = 0;
	BG3_YDX = 0;
	BG3_YDY = 1 << 8;

	//BG3_CX = 128;
	BG3_CY = 0;

/*
	BG2_CR = BG_BMP16_256x256 |
			 BG_BMP_BASE(8) |
			 BG_PRIORITY(2);

	BG2_XDX = 1 << 8;
	BG2_XDY = 0;
	BG2_YDX = 0;
	BG2_YDY = 1 << 8;

	BG2_CX = 0;
	BG2_CY = 0;
*/
	SUB_BG3_CR = BG_BMP16_256x256 |
				 BG_BMP_BASE(0) |
				 BG_PRIORITY(3);

	SUB_BG3_XDX = 1 << 8;
	SUB_BG3_XDY = 0;
	SUB_BG3_YDX = 0;
	SUB_BG3_YDY = 1 << 8;

	SUB_BG3_CX = 0;
	SUB_BG3_CY = 16 << 8;

}

void displayBackground(){

	dmaCopyHalfWords(DmaChannel,
					 caveBitmap,
					 (uint16 *)BG_BMP_RAM(8),
					 caveBitmapLen);

	/**dmaCopyHalfWords(DmaChannel,
					 batBitmap,
					 (uint16 *)BG_BMP_RAM(8),
					 batBitmapLen);
					 */
	
	dmaCopyHalfWords(DmaChannel,phoneBackgroundBitmap,(uint16 *)(BG_BMP_RAM_SUB(0) + 8*1024),phoneBackgroundBitmapLen);
	 
}

void updateInput(){
	scanKeys();
}

void displaySplash(){
	int splash = 1;
	dmaCopyHalfWords(DmaChannel, splashScreenBitmap, (uint16 *)BG_BMP_RAM(8),splashScreenBitmapLen);
	//BG3_CY = -100;
	while(splash){
		updateInput();
		if(keysDown() & KEY_START){
			splash = 0;
		}
	}
}



int main(){
	powerON(POWER_ALL_2D);

	irqInit();
	irqEnable(IRQ_VBLANK);

	lcdMainOnBottom();
	initVideo();
	initBackgrounds();

	lnSpritesInit();
	
	mainmenu Menu;
	Menu.displaySplash();
	return 0;
}